package com.xhd.servlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.xhd.base.util.Date;
import com.xhd.server.ServerConfig;
import com.xhd.util.DataFormat;

public class ServletMgr {
	private static ServletMgr jm16;
	private HashMap jm99 = new HashMap(8);

	public static synchronized ServletMgr getInstance() {
		if (jm16 == null)
			jm16 = new ServletMgr();
		return jm16;
	}

	public void registerServlet(HttpServlet paramHttpServlet) {
		if (this.jm99.containsKey(paramHttpServlet.getServletName()))
			return;
		Item localItem = new Item(null);
		localItem.jm586 = paramHttpServlet.getServletName();
		String str = paramHttpServlet.getInitParameter("thread-max");
		if ((str != null) && (str.length() > 0))
			localItem.jm588 = Integer.parseInt(str);
		str = paramHttpServlet.getInitParameter("log-to-action");
		if ((str != null) && (str.length() > 0))
			localItem.jm587 = "true".equalsIgnoreCase(str);
		str = paramHttpServlet.getInitParameter("per-session-max");
		if ((str != null) && (str.length() > 0)) {
			localItem.jm589 = Integer.parseInt(str);
			if (localItem.jm589 > 0)
				localItem.jm591 = new HashMap(256);
		}
		str = paramHttpServlet.getInitParameter("allow-ips");
		if ((str != null) && (str.length() > 0)) {
			localItem.jm592 = new HashMap(8);
			localItem.jm593 = new ArrayList(4);
			String[] arrayOfString = DataFormat.splitString(str, ',');
			for (int i = 0; i < arrayOfString.length; ++i)
				if (arrayOfString[i].endsWith("."))
					localItem.jm593.add(arrayOfString[i]);
				else
					localItem.jm592.put(arrayOfString[i], "");
		}
		this.jm99.put(paramHttpServlet.getServletName(), localItem);
	}

	public int servletBegin(HttpServlet paramHttpServlet,
			HttpServletRequest paramHttpServletRequest) {
		Item localItem = (Item) this.jm99
				.get(paramHttpServlet.getServletName());
		if (localItem != null) {
			if (!(jm98(localItem, paramHttpServletRequest)))
				return 403;
			if ((localItem.jm588 > 0)
					&& (localItem.jm588 <= localItem.jm590.size()))
				return 503;
			if (localItem.jm591 != null) {
				String str = jm97(paramHttpServletRequest);
				if (str != null) {
					Integer localInteger = (Integer) localItem.jm591.get(str);
					if (localInteger != null) {
						if (localInteger.intValue() >= localItem.jm589)
							return 408;
						localInteger = new Integer(1 + localInteger.intValue());
					} else {
						localInteger = new Integer(1);
					}
					localItem.jm591.put(str, localInteger);
				}
			}
			localItem.jm590.put(paramHttpServletRequest, new Long(System
					.currentTimeMillis()));
		}
		return 200;
	}

	public void servletEnd(HttpServlet paramHttpServlet,
			HttpServletRequest paramHttpServletRequest) {
		Item localItem = (Item) this.jm99
				.get(paramHttpServlet.getServletName());
		if (localItem == null)
			return;
		Object localObject1;
		Object localObject2;
		if (localItem.jm591 != null) {
			localObject1 = jm97(paramHttpServletRequest);
			if (localObject1 != null) {
				localObject2 = (Integer) localItem.jm591.get(localObject1);
				if (localObject2 != null) {
					if (((Integer) localObject2).intValue() > 0)
						localObject2 = new Integer(((Integer) localObject2)
								.intValue() - 1);
					else
						localObject2 = new Integer(0);
					localItem.jm591.put(localObject1, localObject2);
				}
			}
		}
		if (localItem.jm587) {
			localObject1 = (Long) localItem.jm590
					.remove(paramHttpServletRequest);
			if (localObject1 == null)
				return;
			localObject2 = new ClientAction(1);
			((ClientAction) localObject2).setActionTime(((Long) localObject1)
					.longValue());
			((ClientAction) localObject2).setServiceName("Servlet");
			((ClientAction) localObject2).setMethodName(localItem.jm586);
			String str = paramHttpServletRequest
					.getHeader(ServerConfig.ProxyClientIPHeader);
			if (str == null)
				str = paramHttpServletRequest.getRemoteAddr();
			((ClientAction) localObject2).setClientIP(str);
			((ClientAction) localObject2).log();
		} else {
			localItem.jm590.remove(paramHttpServletRequest);
		}
	}

	public Vector getRequestList() {
		if (this.jm99.size() < 1)
			return null;
		int i = 0;
		Vector localVector1 = new Vector(64);
		Iterator localIterator = this.jm99.values().iterator();
		while (localIterator.hasNext()) {
			Item localItem = (Item) localIterator.next();
			Vector localVector2 = new Vector(localItem.jm590.keySet());
			i = localVector2.size();
			for (int j = 0; j < i; ++j) {
				HttpServletRequest localHttpServletRequest = (HttpServletRequest) localVector2
						.get(j);
				Long localLong = (Long) localItem.jm590
						.get(localHttpServletRequest);
				if (localLong == null)
					continue;
				Hashtable localHashtable = new Hashtable(4);
				localHashtable.put("classname", localItem.jm586);
				localHashtable.put("actionname", localHttpServletRequest
						.getRequestURI());
				String str = localHttpServletRequest
						.getHeader(ServerConfig.ProxyClientIPHeader);
				if (str == null)
					str = localHttpServletRequest.getRemoteAddr();
				localHashtable.put("client-ip", str);
				localHashtable
						.put("starttime", new Date(localLong.longValue()));
				localVector1.add(localHashtable);
			}
		}
		return localVector1;
	}

	private String jm97(HttpServletRequest paramHttpServletRequest) {
		HttpSession localHttpSession = paramHttpServletRequest
				.getSession(false);
		if (localHttpSession != null)
			return localHttpSession.getId();
		return null;
	}

	private boolean jm98(Item paramItem,
			HttpServletRequest paramHttpServletRequest) {
		if (paramItem.jm592 == null)
			return true;
		String str = paramHttpServletRequest
				.getHeader(ServerConfig.ProxyClientIPHeader);
		if ((str == null) || (str.length() < 1))
			str = paramHttpServletRequest.getRemoteAddr();
		if (paramItem.jm592.containsKey(str))
			return true;
		int i = paramItem.jm593.size();
		for (int j = 0; j < i; ++j)
			if (str.startsWith((String) paramItem.jm593.get(j)))
				return true;
		return false;
	}

	private class Item {
		String jm586;
		boolean jm587;
		int jm588;
		int jm589;
		HashMap jm590;
		HashMap jm591;
		HashMap jm592;
		ArrayList jm593;
		//final ServletMgr jm19;

	  private Item()
		 {
		    //  this.jm19 = 
		     this.jm587 = false;
		      this.jm588 = 0;
		      this.jm589 = 0;
		      this.jm590 = new HashMap(128);
		      this.jm591 = null;
		      this.jm592 = null;
		      this.jm593 = null;
		  }
		Item(Item paramItem) {
			
		}
	}
}
